package com.gulimall.cart.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author Rookie-6688
 * @Description
 * @Create 2021-04-16 21:09
 */
@Configuration
//@EnableConfigurationProperties(ThreadPoolConfigProperties.class)  // 这里因为已经将自定义的配置类加入了容器，所以这里不需要指定配置类加入容器
public class MyThreadConfig {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties threadPoolConfigProperties){ // 参数会自动从容器中获取并赋值
        return new ThreadPoolExecutor(threadPoolConfigProperties.getCoreSize(),
                threadPoolConfigProperties.getMaxSize(), threadPoolConfigProperties.getKeepAliveTime(),
                TimeUnit.SECONDS, new LinkedBlockingQueue<>(10000),
                Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
        // 使用配置类配置的核心线程数、存活时间、最大线程数。阻塞队列最大容纳10000个线程，使用默认的线程工厂，饱和策略是Abort（抛出异常）

    }
}
